package org.Service.B.web;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ComputeController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private DiscoveryClient client;

    @RequestMapping(value = "/sub" ,method = RequestMethod.GET)
    public String sub(@RequestParam Integer a, @RequestParam Integer b) {
//        ServiceInstance instance = client.getLocalServiceInstance();
    	List<String> aas = client.getServices();
    	for(String aa : aas) {
    		client.getInstances(aa).stream().forEach(instance -> {
    			System.out.println(instance.getPort());
    		});
    	}
        Integer r = a - b;
        logger.info("/sub, host:"  + ", service_id:"  + ", result:" + r);
        return "From Service-B, Result is " + r+" \nPort:";
    }

    //call service-A
    @RequestMapping(value="testServiceA",method=RequestMethod.GET)
    public String testServiceB(@RequestParam Integer a,@RequestParam Integer b){
    	RestTemplate restTemplate=new RestTemplate();
    	return restTemplate.getForObject("http://localhost:2222/add?a="+a+"&b="+b, String.class);
    }
}